package com.esteel.settlement.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.esteel.settlement.entity.TbSettPay;

public interface TbSettPayRepository extends JpaRepository<TbSettPay, Long>,JpaSpecificationExecutor<TbSettPay>,ITbSettPayRepository {

	public TbSettPay findByPaySerialNo(String paySerialNo);
		
	@Modifying
	@Query(value="update TB_ORDER_IRON t set t.buy_order_status='5' ,t.sale_order_status='5' ,t.order_status_type='3', t.buy_operate='', t.sale_operate='上传放货单' where t.id in(  select t.id from TB_ORDER_IRON  t where t.ORDER_NO =?1)",nativeQuery=true)
	public int payment(String orderNo);

	@Modifying
	@Query(value="update TB_ORDER_IRON t set t.buy_order_status='13' ,t.sale_order_status='13' ,t.order_status_type='5', t.buy_operate='已完成', t.sale_operate='已完成' where t.id in(  select t.id from TB_ORDER_IRON  t where t.ORDER_NO =?1)",nativeQuery=true)
	public int payFinal(String orderNo);

	@Modifying
	@Query(value="insert into TB_ORDER_LOG(id,user_type,operate,operate_time,order_no) values(SEQ_TB_ORDER_LOG.nextval,'2','付货款',sysdate,?1)",nativeQuery=true)
	public int logPayment(String orderNo);
	
	@Modifying
	@Query(value="insert into TB_ORDER_LOG (id, user_type, operate, operate_time, order_no) values   (SEQ_TB_ORDER_LOG.nextval, (select decode(t.final_pay_payer,t.sale_id,'1',t.buy_id,'2','3') from tb_order_iron t where t.order_no = ?1 and rownum<2), '付尾款', sysdate, ?1)",nativeQuery=true)
	public int logPayFinal(String orderNo);

	@Modifying
	@Query(value="update TB_ORDER_IRON t set t.buy_order_status='4' ,t.sale_order_status='4' ,t.order_status_type='3', t.buy_operate='付货款', t.sale_operate='' where t.id in(  select t.id from TB_ORDER_IRON  t where t.ORDER_NO =?1)",nativeQuery=true)
	public int payDeposit(String orderNo);

	@Modifying
	@Query(value="insert into TB_ORDER_LOG (id, user_type, operate, operate_time, order_no) values   (SEQ_TB_ORDER_LOG.nextval, '2', '付订金', sysdate, ?1)",nativeQuery=true)
	public int logpayDeposit(String orderNo);

	@Modifying
	@Query(value="update TB_ORDER_IRON t set t.buy_order_status='4' ,t.sale_order_status='4' ,t.order_status_type='2', t.buy_operate='付货款', t.sale_operate='' where t.id in(  select t.id from TB_ORDER_IRON  t where t.ORDER_NO =?1)",nativeQuery=true)
	public int paymentNoGoods(String orderNo);

	@Modifying
	@Query(value="insert into TB_ORDER_LOG (id, user_type, operate, operate_time, order_no) values   (SEQ_TB_ORDER_LOG.nextval, '2', '付货款', sysdate, ?1)",nativeQuery=true)

	public int logPaymentNoGoods(String orderNo);
}
